<?php
class ProfitBase extends CS_Controller
{
    public function _init(){
        $this->load->model('profit_base_model', 'profitbase');
        $this->load->model('profit_base_percentage_model', 'profitbasepercentage');
        $this->load->model('user_type_model', 'user_type');
    }
    
    public function index(){
        $result = $this->profitbase->search_list();
        if($result && is_array($result)){
            foreach ($result as $key=>$row){
                $result[$key]->list = $this->profitbasepercentage->get_list($row->profit_id);
            }
        }
        $data['result'] = $result;
        $data['list'] = $this->user_type->get_list();
        $this->load->view('profitbase/index',$data);
    }
    
    public function add(){
        $product_type = $this->input->post('product_type');
        $source = $this->input->post('source');
        $type_id = $this->input->post('type_id');
        $percentage = $this->input->post('percentage');
        $amount  = 0;
        foreach ($percentage as $value ){
        	$amount +=$value;
        }
        if(bccomp($amount,100) != 0){
            $this->error('profitbase/index', '', '比率相加不等于100%');
        }
        $this->db->trans_start();
        $resultId = $this->profitbase->add(array('product_type'=>$product_type,'source'=>$source));
        if($resultId){
            $profit_id = $this->db->insert_id();
            foreach ($percentage as $key=>$value){
                $data =array(
                	   'profit_id'=>$profit_id,
                       'percentage'=>$value,
                       'type_id'=>$type_id[$key],
                );
                $this->profitbasepercentage->add($data);
            }
        }
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === FALSE) {
             $this->error('profitbase/index', '', '添加失败！');
        } else {
            $this->success('profitbase/index', '', '添加成功！');
        }
    }
    
    public function ajaxupdate(){
        $profit_id = trim($this->input->post('profit_id'));
        $data['result'] = $this->profitbase->find($profit_id);
        echo $this->load->view('profitbase/update',$data, true);exit;
    }
    
    
    public function update(){
        $profit_id = $this->input->post('profit_id');
        $product_type = $this->input->post('product_type');
        $source = $this->input->post('source');
        $type_id = $this->input->post('type_id');
        $percentage = $this->input->post('percentage');
        $amount  = 0;
        foreach ($percentage as $value ){
            $amount +=$value;
        }
        if(bccomp($amount,100) != 0){
            $this->error('profitbase/index', '', '比率相加不等于100%');
        }
        $this->db->trans_start();
        $resultId = $this->profitbase->update(array('product_type'=>$product_type,'source'=>$source,'profit_id'=>$profit_id));
        $this->profitbasepercentage->delete($profit_id);
        if($resultId){
            foreach ($percentage as $key=>$value){
                $data =array(
                    	    'profit_id'=>$profit_id,
                            'percentage'=>$value,
                            'type_id'=>$type_id[$key],
                );
                $this->profitbasepercentage->add($data);
            }
        }
        $this->db->trans_complete();
    
        if ($this->db->trans_status() === FALSE) {
            $this->error('profitbase/index', '', '修改失败！');
        } else {
            $this->success('profitbase/index', '', '修改成功！');
        }
    }
    
    
    public function delete(){
        $profit_id = $this->input->get('profit_id');
        $is_delete = $this->profitbase->delete($profit_id);
        if ($is_delete) {
            $this->success('profitbase/index', '', '删除成功！');
        } else {
             $this->error('profitbase/index', '', '删除失败！');
        }
    }
}